package com.team.sion.service.Impl;

import com.team.sion.mapper.ConsumerMapper;
import com.team.sion.pojo.Consumer;
import com.team.sion.pojo.ConsumerExample;
import com.team.sion.pojo.SongListExample;
import com.team.sion.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConsumerServiceImpl implements ConsumerService {
    @Autowired
    private ConsumerMapper consumerMapper;
    @Override
    public List<Consumer> findByUsernameAndPassword(Consumer consumer) {
        ConsumerExample example = new ConsumerExample();
        example.createCriteria().andUsernameEqualTo(consumer.getUsername()).andPasswordEqualTo(consumer.getPassword());
        return consumerMapper.selectByExample(example);
    }

    @Override
    public boolean addUser(Consumer consumer) {
        ConsumerExample example = new ConsumerExample();
        example.createCriteria().andUsernameEqualTo(consumer.getUsername());
        List<Consumer> list = consumerMapper.selectByExample(example);
        if(list.size() == 0){
            consumerMapper.insertSelective(consumer);
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteUser(Integer id) {
        ConsumerExample example = new ConsumerExample();
        example.createCriteria().andIdEqualTo(id);
        consumerMapper.deleteByExample(example);
        return true;
    }

    @Override
    public boolean updateUser(Consumer consumer) {
        int i = consumerMapper.updateByPrimaryKeySelective(consumer);
        if(i == 1){
            return true;
        }
        return false;
    }

    @Override
    public boolean updatePassword(Consumer consumer) {
        ConsumerExample example = new ConsumerExample();
        example.createCriteria().andPasswordEqualTo(consumer.getOld_password()).andIdEqualTo(consumer.getId());
        List<Consumer> consumers = consumerMapper.selectByExample(example);
        if(consumers.size() > 0){
            int i = consumerMapper.updateByPrimaryKeySelective(consumer);
            return true;
        }
        return false;
    }

    @Override
    public List<Consumer> detail(Integer id) {
        ConsumerExample example = new ConsumerExample();
        example.createCriteria().andIdEqualTo(id);
        return consumerMapper.selectByExample(example);
    }

    @Override
    public void updatePic(Integer id, String url) {
        Consumer consumer = new Consumer();
        consumer = consumerMapper.selectByPrimaryKey(id);
        consumer.setAvator(url);
        consumerMapper.updateByPrimaryKey(consumer);
    }

    @Override
    public List<Consumer> findAll() {
        ConsumerExample example = new ConsumerExample();
        return consumerMapper.selectByExample(example);
    }
}
